package com.cqut.controller;


import com.cqut.entity.User;
import com.cqut.service.UserService;
import com.cqut.util.ResultVOUtil;
import com.cqut.VO.result.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 非空 前端控制器
 * </p>
 *
 * @author 冯永龙
 * @since 2021-06-15
 */
@RestController
@RequestMapping("//user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResultVO<User> login(@RequestBody User user) {
        User resultUser = userService.login(user);
        if (resultUser != null) {
            return ResultVOUtil.success(resultUser);
        }
        return ResultVOUtil.fail();
    }

    @PostMapping("/register")
    public ResultVO<Object> register(@RequestBody User user) {
        boolean result = userService.register(user);
        if (result) {
            return ResultVOUtil.success(null);
        }
        return ResultVOUtil.fail();
    }

    @PostMapping("/updateBaseInfo")
    public ResultVO<User> updateBaseUserInfo(@RequestBody User user) {
        if (userService.updateUserBaseInfo(user)) {
            return ResultVOUtil.success(userService.findUserById(user.getId()));
        }
        return ResultVOUtil.fail();
    }

    @PostMapping("/updateSettingInfo")
    public ResultVO<User> updateSettingInfo(@RequestBody User user) {
        if (userService.updateSettingInfo(user)) {
            return ResultVOUtil.success(userService.findUserById(user.getId()));
        }
        return ResultVOUtil.fail();
    }

    @PostMapping("/updateBindingAccount")
    public ResultVO<User> updateBindingAccount(@RequestBody User user) {
        if (userService.updateBindingAccount(user)) {
            return ResultVOUtil.success(userService.findUserById(user.getId()));
        }
        return ResultVOUtil.fail();
    }

}

